home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_319 / cnewssrc / cnews.orig.lzh / rna / newsrc.c < prev    next >
C/C++ Source or Header  |  1989-06-27  |  3KB  |  123 lines

  1. /*
  2.  * newsrc file handling
  3.  */
  4.  
  5. #include "defs.h"
  6.  
  7. static char nrcname[]     = NEWSRC;
  8.  
  9. static char *rcname;        /* full pathname of .newsrc */
  10. newsrc *rc;            /* internal .newsrc */
  11. char *rcgrps;            /* subscription from .newsrc */
  12. static newsrc *lastrc;        /* last newsrc struct in list */
  13. static int rclineno;        /* current lineno in .newsrc */
  14. static bool sortrc;        /* if we should sort on output */
  15.  
  16. static newsrc *findnewsrc();
  17.  
  18. readnewsrc()
  19. {
  20.     register FILE *f;
  21.     static char option[] = "options";
  22.     char word[BUFSIZ], rest[BUFSIZ];
  23.     extern char *getenv();
  24.  
  25.     if ((rcname = getenv("HOME")) == NULL)
  26.         error("No $HOME in environment.");
  27.     rcname = newstr3(rcname, "/", nrcname);
  28.     if ((f = fopen(rcname, "r")) == NULL)
  29.         return;
  30.  
  31.     rclineno = 0;
  32.     while (getline(f, word, rest))
  33.         if (CMP(word, option) == 0)
  34.             dooptions(rest);
  35.         else
  36.             dorcline(word, rest);
  37.     (void) fclose(f);
  38. }
  39.  
  40. /*
  41.  * Read a line from f, put first word into w and the rest into r.
  42.  * Discard trailing newline instead of storing it.
  43.  * This is a poor design, as w & r are unchecked for overrun.
  44.  */
  45. static
  46. getline(f, w, r)
  47. register FILE *f;
  48. char *w, *r;
  49. {
  50.     register int c;
  51.     register char *s;
  52.  
  53.     rclineno++;
  54.     s = w;
  55.     while ((c = getc(f)) != EOF && c != ' ' && c != '\t')
  56.         *s++ = c;            /* stash first word */
  57.     *s = '\0';
  58.  
  59.     if (c != EOF) {
  60.         s = r;
  61.         while ((c = getc(f)) != EOF && c != '\n')
  62.             *s++ = c;        /* stash the rest */
  63.         *s = '\0';
  64.     }
  65.  
  66.     if (c != '\n' && c != EOF)
  67.         error("Bad format: %s line %d: %s", rcname, rclineno, w);
  68.  
  69.     return c != EOF;
  70. }
  71.  
  72. /*
  73.  * Parse s into words and simulate command line arguments with them.
  74.  */
  75. static
  76. dooptions(s)
  77. char *s;
  78. {
  79.     register char *cp;
  80.     register int argc;
  81.     register char **argv;
  82.  
  83.     cp = s;
  84.     while (isspace(*cp))
  85.         cp++;
  86.     if (!*cp)
  87.         return;
  88.  
  89.     argc = 1;
  90.     argv = (char **) myalloc(sizeof(char *));
  91.     argv[argc - 1] = cp;
  92.     while (*cp && (cp = strpbrk(cp, " \t")) != NULL) {
  93.         while (*cp == ' ' || *cp == '\t')
  94.             *cp++ = '\0';
  95.         if (*cp) {
  96.             argc++;
  97.             argv = (char **) myrealloc((char *) argv,
  98.                 argc * (int)sizeof(char *));
  99.             argv[argc - 1] = cp;
  100.         }
  101.     }
  102.     if (options(argc, argv, false))
  103.         error("Bad options: %s line %d: %s", rcname, rclineno, s);
  104.     free((char *) argv);
  105. }
  106.  
  107. /*
  108.  * Parse w & r together as a .newsrc newsgroup line.
  109.  */
  110. static
  111. dorcline(w, r)
  112. char *w, *r;
  113. {
  114.     register char lastw;
  115.     register int len;
  116.     register newsrc    *np;
  117.  
  118.     len = strlen(w);
  119.     lastw = w[len - 1];            /* save presumed colon or bang */
  120.     w[len - 1] = '\0';            /* nuke presumed colon */
  121.     while (*r == ' ' || *r == '\t')
  122.         r++;                /* skip extra whitespace *
  123.